package cn.qy.kotlin.base.ch12

import cn.qy.kotlin.base.ch10.Player

/**
 * 继承
 */
fun main() {
    var currentRoom = Room("Foyer")
    println(currentRoom.description())
    println(currentRoom.load())

    currentRoom = TownSquare()
    println(currentRoom.description())
    println(currentRoom.load())

    currentRoom = Piazza()
    println(currentRoom.description())
    println(currentRoom.load())

    val className = when (currentRoom) {
        is TownSquare -> "TownSquare"
        is Room -> "Room"
        else -> throw IllegalArgumentException()
    }
    println(className)

    printIsSourceOfBlessings(currentRoom)
}

fun printIsSourceOfBlessings(any: Any) {
    val isSourceOfBlessings = if (any is Player) {
        any.isBlessed
    } else {
        // as 操作符表明，这是一个类型转换。可以认为是 Java 中的强转，需要保证别发生类型转换异常
        (any as Room).name == "Fount of Blessings"
    }
    // Any 类提供了超越类的一种抽象定义
    // 所以，如果项目代码的目标平台是 JVM， Any 类的 toString 函数的实现版本就是 java.lang.Object.toString
    println("$any is a source of blessings: $isSourceOfBlessings")
}